﻿using System;
using Foothill.Common;
using Foothill.Model;
using System.Configuration;

namespace Foothill.Service.Configuration
{
    public class HostSettingManager : IHostSettingManager
    {
        private readonly ICustomConfigurationManager _customConfigurationManager;

        private readonly string _connStrName = "FoothillDB";

        public HostSettingManager(ICustomConfigurationManager customConfigurationManager)
        {
            _customConfigurationManager = customConfigurationManager;
        }

        public string GetSiteBaseUrlString()
        {
            string baseUrl = HostSettings.BaseUrlStringDev;
            FoothillConfigurationSection customSection = _customConfigurationManager.GetFoothillConfigurationSection();

            if (customSection != null)
            {
                DeploymentElement dep = customSection.Deployment;

                switch (dep.DeploymentName)
                {
                    case "dev":
                        baseUrl = HostSettings.BaseUrlStringDev;
                        break;
                    case "ArvixeDev":
                        baseUrl = HostSettings.BaseUrlStringArvixeDev;
                        break;
                    case "prod":
                        baseUrl = HostSettings.BaseUrlStringProd;
                        break;
                }
            }

            return baseUrl;
        }

        public Uri GetSiteBaseUri()
        {
            return new Uri(GetSiteBaseUrlString());
        }

        public string GetConnectedDatabaseTypeName()
        {
            FoothillDatabaseType dbType = GetDatabaseType();
            string dbTypeName = "Unknown";
            
            switch (dbType)
            {
                case FoothillDatabaseType.LocalDev:
                    dbTypeName = @"(local)\SQLEXPRESS";
                    break;
                case FoothillDatabaseType.Production:
                    dbTypeName = "calantha.arvixe.com";
                    break;
            }

            return dbTypeName;
        }

        internal FoothillDatabaseType GetDatabaseType()
        {
            string connStr = ConfigurationManager.ConnectionStrings[_connStrName].ConnectionString;
            string dataSource = GetDataSourceFromConnectionString(connStr);
            FoothillDatabaseType databaseType = FoothillDatabaseType.Unknown;
            if (string.Compare(dataSource, "calantha.arvixe.com") == 0)
            {
                databaseType = FoothillDatabaseType.Production;
            }
            else if (string.Compare(dataSource, @"(local)\SQLEXPRESS") == 0)
            {
                databaseType = FoothillDatabaseType.LocalDev;
            }

            return databaseType;
        }

        internal string GetDataSourceFromConnectionString(string connStr)
        {
            if (string.IsNullOrWhiteSpace(connStr))
            {
                return string.Empty;
            }

            string[] tokens = connStr.Split(';');
            string dataSource = string.Empty;;
            foreach (string token in tokens)
            {
                if (token.Contains("Data Source") || token.Contains("data source"))
                {
                    string[] strings = token.Split('=');
                    if (strings.Length == 2)
                    {
                        dataSource = strings[1];
                    }

                    break;
                }
            }

            return dataSource;
        }
    }
}
